<template>
  <div>
    <el-form ref="formMatter" :model="formMatter" :inline="true">
      <el-form-item label="顾客名称" prop="customerName">
        <el-select
          v-model="formMatter.customerName"
          placeholder="请输入顾客名称"
          filterable
          allow-create
          default-first-option
          clearable
        >
          <el-option
            v-for="item in customerList"
            :key="item.id"
            :label="item.customerName"
            :value="item.customerName"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="钢种" prop="steelGradeId">
        <el-select v-model="formMatter.steelGradeId" size="mini" filterable allow-create default-first-option clearable>
          <el-option v-for="item in steelGradeList" :key="item.id" :value="item.steelGradeName"
            :label="item.steelGradeName" />
        </el-select>
      </el-form-item>
      <el-form-item label="钢类" prop="steelTypeId">
        <el-select v-model="formMatter.steelTypeId" clearable size="mini">
          <el-option v-for="item in steelTypes" :key="item.id" :value="item.steelTypeName" :label="item.steelTypeName" />
        </el-select>
      </el-form-item>
      <el-form-item label="日期范围" prop="startDate">
        <el-date-picker v-model="formMatter.startDate" value-format="yyyy-MM-dd" type="date" placeholder="开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="endDate">
        <el-date-picker v-model="formMatter.endDate" value-format="yyyy-MM-dd" type="date" placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="编号" prop="code">
        <el-input v-model="formMatter.code" size="mini" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="getHistoricalReviews()">查询</el-button>
        <el-button type="success" size="mini"
          @click="() => { $refs.formMatter.resetFields(); handleCurrentChange(1) }">重置</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-card style="height: 100%">
        <div slot="header">
          <span>历史评审</span>
          <el-button style="float: right" type="warning" size="mini" @click="download()">下载</el-button>
        </div>
        <hmi-table :table-data="tableData" :is-pagination-show="true" :table-height="'calc(100vh - 230px)'"
          :table-head="true" :total="total" :current-page="formMatter.current" :page-size="formMatter.page"
          index-tyle="index" @handle-selection-change="handleSelectionChange" @handle-size-change="handleSizeChange" @handle-current-change="handleCurrentChange">
          <template slot="TableBody">
            <el-table-column type="selection" width="55" align="center"> </el-table-column>
            <el-table-column label="序号" type="index" width="70" align="center"></el-table-column>
            <el-table-column label="编号" align="center" prop="code"></el-table-column>
            <el-table-column label="顾客名称" align="center" prop="customerName" width="200"></el-table-column>
            <el-table-column label="顾客联系方式" align="center" width="160" prop="customerPhone"></el-table-column>
            <el-table-column label="钢类" align="center" prop="steelTypeName"></el-table-column>
            <el-table-column label="钢种" align="center" prop="steelGradeName" show-overflow-tooltip></el-table-column>
            <el-table-column label="标准" align="center" prop="standardName" width="190" show-overflow-tooltip></el-table-column>
            <el-table-column label="规格" align="center" prop="specification"></el-table-column>
            <el-table-column label="数量" align="center" prop="steelNumber">
              <template slot-scope="scope">
                {{ scope.row.steelNumber }}{{ scope.row.steelNumberUnit }}
              </template>
            </el-table-column>
            <el-table-column label="交货状态" align="center" prop="deliveryStatus"></el-table-column>
            <el-table-column label="加工用途" align="center" prop="processingPurpose"></el-table-column>
            <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
              <template slot-scope="scope">
                <span>{{ formatterTime(scope.row.submitTime) }}</span>
              </template></el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scoped">
                <el-button type="text" size="mini" @click="getPendingOrders(scoped.row)">查看</el-button>
              </template>
            </el-table-column>
          </template>
        </hmi-table>
      </el-card>
    </div>

    <!-- 弹窗显示详情 -->
    <el-dialog
  :visible.sync="dialogVisible"
  :before-close="handleClose"
  title="历史订单查看"
  width="700px">
  <order-detail :form-data="orderInfo" :show-edit-btn="0" style="margin: auto" _width="600px"/>
  <span slot="footer" class="dialog-footer">
    <el-button size="small" @click="dialogVisible = false">取 消</el-button>
  </span>
</el-dialog>
  </div>
</template>

<script>
import HmiTable from "@/components/HmiTable.vue";
import { timeChange } from "@/lib/FormatTime";
import {
  GET_SALE_HISTORICALREVIEW,
  GET_STEELGRADELIST,
  GET_CUSTOMERNAMELIST,
  GET_STEELTYPELIST,
  FileDownLoad,
  getReviewedLeftList
} from "@/lib/Api.js";
import OrderDetail from "@/components/OrderDetail.vue";
import { post, get, downloadFile } from "@/lib/Util.js";
export default {
  components: {
    HmiTable,
    OrderDetail
  },
  data() {
    return {
      total: 0,
      formMatter: {
        code: "",
        current: 1,
        customerName: "",
        endDate: "",
        page: 5,
        startDate: "",
        steelGradeId: "",
        steelTypeId: ""
      },
      statuses: [],
      tableData: [],
      steelGradeList: [],
      steelTypes: [],
      customerList: [],
      selectDataId: [],
      dialogVisible: false,
      orderInfo: {}
    };
  },
  mounted() {
    this.getHistoricalReviews(); // 组件挂载后立即执行
    this.getSteelGradeName();
    this.getCustomerNames(); // 获取顾客
    this.getsteelTypeNames();
  },
  methods: {
    handleClose() {
      this.dialogVisible = false;
    },
    getPendingOrders(row) {
      post(getReviewedLeftList, { contractInfoId: row.id }).then(resp => {
        if (resp.data) {
          this.orderInfo = resp.data;
          this.dialogVisible = true;
        }
      });
    },
    getSteelGradeName() {
      post(GET_STEELGRADELIST, { steelGradeName: "" }).then(resp => {
        this.steelGradeList = resp.data;
      });
    },
    /**
     * 格式化日期方法
     */
    formatterTime(time) {
      if (time !== "" && time !== undefined) {
        return timeChange(time);
      } else {
        return "";
      }
    },
    getHistoricalReviews() {
      post(GET_SALE_HISTORICALREVIEW, this.formMatter).then(resp => {
        const { total, rows } = resp;
        this.total = total;
        this.tableData = rows;
      });
    },

    handleCurrentChange(current) {
      this.formMatter.current = current;
      this.getHistoricalReviews();
    },
    handleSizeChange(page) {
      this.formMatter.page = page;
      this.handleCurrentChange(1);
    },
    handleSelectionChange(selection) {
      const id = [];
      selection.map(item => {
        id.push(Number(item.id));
      });
      this.selectDataId = id;
    },
    // 批量下载
    async download() {
      downloadFile(FileDownLoad + "/" + this.selectDataId, "");
    },
    // 获取顾客
    getCustomerNames() {
      post(GET_CUSTOMERNAMELIST, { customerName: "" }).then(resp => {
        this.customerList = resp.data;
      });
    },
    // 获取钢类
    getsteelTypeNames() {
      post(GET_STEELTYPELIST, { steelTypeName: "" }).then(resp => {
        this.steelTypes = resp.data;
      });
    }
  }
};
</script>

<style scoped>
</style>
